package com.hou.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hou.eduservice.entity.EduTeacher;
import com.hou.eduservice.mapper.EduTeacherMapper;
import com.hou.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import sun.text.normalizer.NormalizerBase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-05
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    // 获取前四名热门讲师
    @Override
    public List<EduTeacher> getHotTeacher() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");

        List<EduTeacher> eduTeachers = baseMapper.selectList(wrapper);

        return eduTeachers;
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> teacherPage) {

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");

        baseMapper.selectPage(teacherPage, queryWrapper);

        // 每页数据集合
        List<EduTeacher> records = teacherPage.getRecords();
        // 当前页
        long current = teacherPage.getCurrent();
        // 总页数
        long pages = teacherPage.getPages();
        // 每页记录数
        long size = teacherPage.getSize();
        // 总记录数
        long total = teacherPage.getTotal();

        // 是否有下一页
        boolean hasNext = teacherPage.hasNext();
        // 是否有上一页
        boolean hasPrevious = teacherPage.hasPrevious();

        HashMap<String, Object> map = new HashMap<>();

        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;


    }

}
